package com.ruoyi.crm.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.oss.core.OssClient;
import com.ruoyi.common.oss.entity.UploadResult;
import com.ruoyi.common.oss.factory.OssFactory;
import com.ruoyi.crm.domain.BusinessOpportunity;
import com.ruoyi.crm.mapper.BusinessOpportunityMapper;
import com.ruoyi.crm.service.IBusinessOpportunityService;
import com.ruoyi.system.api.RemoteUserService;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Service
public class BusinessOpportunityServiceImpl implements IBusinessOpportunityService {
    private final BusinessOpportunityMapper businessOpportunityMapper;



    /**
     * 分页查询商机列表
     * @param businessOpportunity
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<BusinessOpportunity> selectProspectPageList(BusinessOpportunity businessOpportunity, PageQuery pageQuery) {
        LambdaQueryWrapper<BusinessOpportunity> queryWrapper = new LambdaQueryWrapper<>();
        //模糊查询商机名称
        if(StringUtils.isNotBlank(businessOpportunity.getProspectName())){
            queryWrapper.like(BusinessOpportunity::getProspectName, businessOpportunity.getProspectName());
        }
        //查询商机状态
        if(StringUtils.isNotBlank(businessOpportunity.getOpportunityStatus())){
            queryWrapper.eq(BusinessOpportunity::getOpportunityStatus, businessOpportunity.getOpportunityStatus());

        }

        Page<BusinessOpportunity> page = businessOpportunityMapper.selectPage(pageQuery.build(), queryWrapper);
        return TableDataInfo.build(page);
    }
    /**
     * 根据商机列表ID获取详细信息
     *
     * @param prospectId 商机列表ID
     */
    @Override
    public BusinessOpportunity selectProspectById(Long prospectId) {
        return businessOpportunityMapper.selectById(prospectId);
    }

    /**
     * 新增商机列表
     * @param businessOpportunity
     * @return
     */
    @Override
    public int insertProspectById(BusinessOpportunity businessOpportunity) {
        return businessOpportunityMapper.insert(businessOpportunity);
    }

    /**
     * 修改商机列表
     * @param businessOpportunity
     * @return
     */
    @Override
    public int updateProspectById(BusinessOpportunity businessOpportunity) {
        return businessOpportunityMapper.updateById(businessOpportunity);
    }

    /**
     * 删除商机列表
     * @param prospectId
     * @return
     */

    @Override
    public int deleteProspectById(Long prospectId) {
        return businessOpportunityMapper.deleteById(prospectId);
    }

    @Override
    public List<BusinessOpportunity> selectProspectByList(BusinessOpportunity businessOpportunity) {
        LambdaQueryWrapper<BusinessOpportunity> queryWrapper = new LambdaQueryWrapper<>();
        //模糊查询商机名称
        if(StringUtils.isNotBlank(businessOpportunity.getProspectName())){
            queryWrapper.like(BusinessOpportunity::getProspectName, businessOpportunity.getProspectName());
        }
        //查询商机状态
        if(StringUtils.isNotBlank(businessOpportunity.getOpportunityStatus())){
            queryWrapper.eq(BusinessOpportunity::getOpportunityStatus, businessOpportunity.getOpportunityStatus());
        }
        return businessOpportunityMapper.selectList(queryWrapper);
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    @Override
    public BusinessOpportunity upload(MultipartFile file) {
        String originalfileName = file.getOriginalFilename();
        String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
        OssClient storage = OssFactory.instance();
        UploadResult uploadResult;
        try {
            uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
        } catch (IOException e) {
            throw new ServiceException(e.getMessage());
        }
        // 保存文件信息
        return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult);
    }
    private BusinessOpportunity buildResultEntity(String originalfileName, String suffix, String configKey, UploadResult uploadResult) {
        BusinessOpportunity oss = new BusinessOpportunity();
        oss.setUrl(uploadResult.getUrl());
        oss.setOriginalName(originalfileName);
        businessOpportunityMapper.insert(oss);
        BusinessOpportunity sysOssVo = BeanUtil.toBean(oss, BusinessOpportunity.class);
        if (oss == null || oss.getUrl() == null) {
            return oss; // 如果为空，直接返回
        }

        String url = oss.getUrl();

        // 这里举例：如果url不是以http开头，自动加上http://
        if (!url.startsWith("http://") && !url.startsWith("https://")) {
            url = "http://" + url;
        }

        oss.setUrl(url);
        return oss;
    }


}
